package com.itheima.service.db.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.*;
import com.itheima.mapper.*;
import com.itheima.service.db.UmsRoleService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

@DubboService
public class UmsRoleServiceImpl implements UmsRoleService {

    @Autowired
    UmsRoleMapper umsRoleMapper;

    @Autowired
    UmsAdminRoleRelationMapper umsAdminRoleRelationMapper;

    @Autowired
    UmsMenuMapper umsMenuMapper;

    @Autowired
    UmsRoleMenuRelationMapper umsRoleMenuRelationMapper;

    @Autowired
    UmsRoleResourceRelationMapper umsRoleResourceRelationMapper;

    @Override
    public UmsRole findById(Long roleId) {
        return umsRoleMapper.selectById(roleId);
    }

    // 获取所有角色
    @Override
    public List<UmsRole> findByAllList() {
        return umsRoleMapper.selectList(null);
    }

    // 获取指定用户的角色
    @Override
    public List<UmsRole> findByRoleList(Long adminId) {
        LambdaQueryWrapper<UmsAdminRoleRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsAdminRoleRelation::getAdminId,adminId);
        List<UmsAdminRoleRelation> umsAdminRoleRelations = umsAdminRoleRelationMapper.selectList(queryWrapper);

        List<UmsRole> umsRoleList = new ArrayList<>();

        for (UmsAdminRoleRelation item : umsAdminRoleRelations) {
            Long roleId = item.getRoleId();
            UmsRole umsRole = umsRoleMapper.selectById(roleId);
            umsRoleList.add(umsRole);
        }
        return umsRoleList;
    }

    @Override
    public void allocationRole(Long adminId, List<Long> roleIds) {
        // 创建条件构造器(根据adminId)进行查询
        LambdaQueryWrapper<UmsAdminRoleRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsAdminRoleRelation::getAdminId,adminId);

        // 调用mapper去查询该条对象的信息
        List<UmsAdminRoleRelation> umsAdminRoleRelations = umsAdminRoleRelationMapper.selectList(queryWrapper);
        umsAdminRoleRelations.forEach(item -> {
            umsAdminRoleRelationMapper.deleteById(item.getId());
        });

        // 删除数据库所对应的值后,进行保存操作
        UmsAdminRoleRelation umsAdminRoleRelation = new UmsAdminRoleRelation();
        for (Long roleId : roleIds) {
            umsAdminRoleRelation.setAdminId(adminId);
            umsAdminRoleRelation.setRoleId(roleId);
            umsAdminRoleRelationMapper.insert(umsAdminRoleRelation);
        }
    }
    // 根据角色名称分页获取角色列表
    @Override
    public PageBeanVo findByList(Integer pageNum, Integer pageSize, String keyword) {
        // 创建分页构造器
        Page<UmsRole> page = new Page<>(pageNum,pageSize);

        // 创建条件查询构造器
        LambdaQueryWrapper<UmsRole> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StrUtil.isNotEmpty(keyword),UmsRole::getName,keyword)
                .or().like(StrUtil.isNotEmpty(keyword),UmsRole::getName,keyword);

        Page<UmsRole> umsAdminPage = umsRoleMapper.selectPage(page, queryWrapper);

        return new PageBeanVo(pageNum,pageSize,umsAdminPage.getTotal(),umsAdminPage.getRecords());
    }

    @Override
    public void roleUpdate(Long id, UmsRole umsRole) {
        umsRole.setId(id);
        umsRoleMapper.updateById(umsRole);
    }

    /**
     * 批量删除角色
     * @param ids
     */
    @Override
    public Integer delete(List<Long> ids) {
        LambdaQueryWrapper<UmsRole> queryWrapper = new LambdaQueryWrapper<>();
        int num = 0;
        if (ids != null) {
            for (Long id : ids) {
                queryWrapper.eq(UmsRole::getId,id);
                UmsRole umsRole = umsRoleMapper.selectOne(queryWrapper);
                if (umsRole.getStatus() == 1) {
                    umsRoleMapper.delete(queryWrapper);
                    num = 1;
                } else {
                    num = 0;
                }
            }
        }
        return num;
    }

    /**
     * 添加角色
     * @param umsRole
     */
    @Override
    public void create(UmsRole umsRole) {
        umsRole.setCreateTime(LocalDateTime.now());
        umsRoleMapper.insert(umsRole);
    }

    /**
     * 给角色分配资源
     * @param resourceIds
     * @param roleId
     */
    @Override
    public void allocResource(List<Integer> resourceIds, Long roleId) {
        // 创建条件构造器
        LambdaQueryWrapper<UmsRoleResourceRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRoleResourceRelation::getRoleId,roleId);
        umsRoleResourceRelationMapper.delete(queryWrapper);
        for (Integer resourceId : resourceIds) {
            UmsRoleResourceRelation umsRoleResourceRelation = new UmsRoleResourceRelation();
            umsRoleResourceRelation.setRoleId(roleId);
            umsRoleResourceRelation.setResourceId(resourceId.longValue());
            umsRoleResourceRelationMapper.insert(umsRoleResourceRelation);
        }
    }

    /**
     * 修改角色状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        LambdaQueryWrapper<UmsRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRole::getId,id);
        UmsRole umsRole = umsRoleMapper.selectOne(queryWrapper);
        if (umsRole != null) {
            umsRole.setStatus(status);
            umsRoleMapper.update(umsRole,queryWrapper);
        }
    }
}
